package cn.ipanel.bigdata.boot.date

/**
 * Author: lzz
 * Date: 2022/3/24 8:47
 */
case class Hour (st: Long, et: Long) extends Date(st, et) {

  override def prev(n: Int = 1): Hour = Hour(st - Date.HOUR_TIME * n)
  override def next(n: Int = 1): Hour = Hour(et + Date.HOUR_TIME * n)
  def toDayIndex: Int = STime.toHour
  override def toString: String = s"Hour(${STime.toYYYYMMddHHmmss} To ${ETime.toYYYYMMddHHmmss})"
}

object Hour {

  def unapply(yyyyMMddHHmmss: String): Option[(Long, Long)] = {
    unapply(Time(yyyyMMddHHmmss, Time._yyyy_MM_dd_HH_mm_ss).time)
  }

  def unapply(time: Long): Option[(Long, Long)] = {
    val ca: java.util.Calendar = java.util.Calendar.getInstance();
    ca.setTime(new java.util.Date(time))
    ca.set(java.util.Calendar.MINUTE, 0)
    ca.set(java.util.Calendar.SECOND, 0)
    ca.set(java.util.Calendar.MILLISECOND, 0)
    Some(ca.getTime.getTime, ca.getTime.getTime + Date.HOUR_TIME - 1)
  }

  def apply(yyyyMMddHH: String): Hour = apply(Time(yyyyMMddHH, "yyyyMMddHH"))
  def apply(time: Time): Hour = apply(time.time)
  def apply(time: Long): Hour = time match {
    case Hour(st, et) => Hour(st, et)
    case _ => null
  }
}